package com.qf.emp.dao.impl;

import com.qf.emp.dao.ProfileDao;
import com.qf.emp.entity.Page;
import com.qf.emp.entity.Profile;
import com.qf.emp.entity.User;
import com.qf.emp.utils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class ProfileDaoImpl implements ProfileDao{
    private QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
    @Override
    public Profile selectPer(int id) {
        String sql = "select * from profile where id = ?;";
        try {
            return queryRunner.query(sql, new BeanHandler<Profile>(Profile.class), id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(Profile per) {
        String sql = "update profile set name=?,birthday=?,gender=?,career=?,address=?,mobile=?,picture=? where id=?";
        try {
            return  queryRunner.update(sql,per.getName(),per.getBirthday(),per.getGender(),per.getCareer(),per.getAddress(),per.getMobile(),per.getPicture(),per.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Profile> selectAll(Page page) {
        String sql = "select * from profile limit ?,?;";
        try {
            return queryRunner.query(sql,new BeanListHandler<Profile>(Profile.class), page.getStartRows(), page.getPageSize());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long selectCount() {
        String sql = "select count(*) from profile;";
        try {
            queryRunner.query(sql,new ScalarHandler<>());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

}
